package com.privated.work.controller;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.privated.work.entity.BusinessFinal;
import com.privated.work.service.BusinessFinalService;

import java.util.ArrayList;
import java.util.List;

public class ImportData extends AnalysisEventListener<BusinessFinal> {
    private BusinessFinalService businessFinalService;

    private List<BusinessFinal> datas;

    private Integer maxCount = 100;

    private Integer allCount = 0;
    public ImportData(BusinessFinalService businessFinalService) {
        this.businessFinalService = businessFinalService;
        this.datas = new ArrayList<>();
    }

    @Override
    public void invoke(BusinessFinal data, AnalysisContext context) {
        datas.add(data);
        if(datas.size() == maxCount){
            businessFinalService.saveBatch(datas);
            datas.clear();
        }
        allCount ++;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if(datas.size() > 0){
            businessFinalService.saveBatch(datas);
        }
        System.out.println("总共读取到数据行数:"+allCount);
    }
}
